<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <!-- 引入 Tailwind CSS -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <!-- 引入 Axios 和 Vue -->
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
</head>

<body class="bg-gradient-to-r from-red-500 to-pink-500 flex items-center justify-center min-h-screen">
    <div class="container bg-white p-8 rounded-lg shadow-lg w-full max-w-sm">
        <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">注册页面</h1>

        <form @submit.prevent="reg" novalidate>
            <input type="text" placeholder="用户名" v-model="user.username" required
                class="w-full p-3 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-pink-400 transition duration-200 ease-in-out" />
            <input type="password" placeholder="密码" v-model="user.password" required
                class="w-full p-3 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-pink-400 transition duration-200 ease-in-out" />
            <input type="text" placeholder="昵称" v-model="user.nickname" required
                class="w-full p-3 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-pink-400 transition duration-200 ease-in-out" />
            <button type="submit"
                class="w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 transition duration-200 ease-in">
                注册
            </button>
            <div v-if="errorMessage" class="error-message text-red-500 mt-3 text-center">{{ errorMessage }}</div>
        </form>

        <div class="mt-4 text-center">
            <p class="text-gray-600 text-sm">已经有账号了？ <a href="/login.html" class="text-blue-500 hover:underline">登录</a></p>
        </div>
    </div>

    <script>
        new Vue({
            el: ".container",
            data() {
                return {
                    user: { username: "", password: "", nickname: "" },
                    errorMessage: ""
                };
            },
            methods: {
                reg() {
                    // 表单验证
                    if (!this.user.username || !this.user.password || !this.user.nickname) {
                        this.errorMessage = "所有字段都是必填的";
                        return;
                    }

                    // 发出注册请求
                    axios.post("/v1/users/reg", this.user)
                        .then(response => {
                            if (response.data === 1) {
                                alert("注册成功");
                                location.href = "/login.html"; // 改为注册成功后的重定向路径
                            } else {
                                this.errorMessage = "用户名已存在";
                            }
                        })
                        .catch(error => {
                            console.error('Registration failed:', error);
                            this.errorMessage = "注册失败，请稍后再试";
                        });
                }
            }
        });
    </script>
</body>

</html>